package FillMythMetadataUtilities;
use strict;

sub trim($)
{
	my $string = shift;
	$string =~ s/^\s+//;
	$string =~ s/\s+$//;
	return $string;
}

sub remove_last_sep
{
	my $path_ = $_[0];
	if ($path_ =~ m/(.+)\/$/gi)
	{
		$path_ = $1;
	}
	return $path_;
}

sub levenshtein
{
	# $s1 and $s2 are the two strings
    # $len1 and $len2 are their respective lengths
    #
    my ($s1, $s2) = @_;
	# Enhancement by Laurie Odgers
    # I want only letters digits and spaces
    $s1 =~ s/[^a-zA-Z\d\s]*//g;
    $s2 =~ s/[^a-zA-Z\d\s]*//g;
	#ofcourse, all should be lowercase.
	$s1 =~ tr/[A-Z]/[a-z]/;
	$s2 =~ tr/[A-Z]/[a-z]/;
	#no n-spaces.	
	$s1 =~ s/\s+/ /g;
	$s2 =~ s/\s+/ /g;
	
	#print "s1: '$s1'. s2: '$s2'\n";
	
    my ($len1, $len2) = (length $s1, length $s2);

    # If one of the strings is empty, the distance is the length
    # of the other string
    #
    return $len2 if ($len1 == 0);
    return $len1 if ($len2 == 0);

    # if the strings match exactly then the distance is 0
    return 0 if ($s1 eq $s2);

    my %mat;

    # Init the distance matrix
    #
    # The first row to 0..$len1
    # The first column to 0..$len2
    # The rest to 0
    #
    # The first row and column are initialized so to denote distance
    # from the empty string
    #
    for (my $i = 0; $i <= $len1; ++$i)
    {
        for (my $j = 0; $j <= $len2; ++$j)
        {
            $mat{$i}{$j} = 0;
            $mat{0}{$j} = $j;
        }

        $mat{$i}{0} = $i;
    }

    # Some char-by-char processing is ahead, so prepare
    # array of chars from the strings
    #
    my @ar1 = split(//, $s1);
    my @ar2 = split(//, $s2);

    for (my $i = 1; $i <= $len1; ++$i)
    {
        for (my $j = 1; $j <= $len2; ++$j)
        {
            # Set the cost to 1 iff the ith char of $s1
            # equals the jth of $s2
            # 
            # Denotes a substitution cost. When the char are equal
            # there is no need to substitute, so the cost is 0
            #
            my $cost = ($ar1[$i-1] eq $ar2[$j-1]) ? 0 : 1;

            # Cell $mat{$i}{$j} equals the minimum of:
            #
            # - The cell immediately above plus 1
            # - The cell immediately to the left plus 1
            # - The cell diagonally above and to the left plus the cost
            #
            # We can either insert a new char, delete a char or
            # substitute an existing char (with an associated cost)
            #
            $mat{$i}{$j} = min([$mat{$i-1}{$j} + 1,
                                $mat{$i}{$j-1} + 1,
                                $mat{$i-1}{$j-1} + $cost]);
        }
    }

    # Finally, the Levenshtein distance equals the rightmost bottom cell
    # of the matrix
    #
    # Note that $mat{$x}{$y} denotes the distance between the substrings
    # 1..$x and 1..$y
    #
    my $lev_result = $mat{$len1}{$len2};
    return $lev_result;
	#Now, I want to soften it a bit
	#So, i'm taking the distance between the strings, but not the added letters.
	#my $string_length_diff = abs($len1 - $len2);
	#return abs($lev_result - $string_length_diff);
}


# minimal element of a list
#
sub min
{
    my @list = @{$_[0]};
    my $min = $list[0];

    foreach my $i (@list)
    {
        $min = $i if ($i < $min);
    }

    return $min;
}

sub no_folder_art_at_folder
{
	my $directory = $_[0];
	if ((-e $directory."folder.jpg") ||
		(-e $directory."folder.png") ||
		(-e $directory."folder.gif") ||
		(-e $directory."folder.bmp"))
	{
		return 0;
	}
	else
	{
		return 1;
	}
}

my $lock_file = "";

sub validate_lock_file
{
	$lock_file = $_[0];
	
	if( length($lock_file) == 0 )
	{
		# No lock file specified ignore it and return
		return;
	}

	if( -e $lock_file )
	{
		print "Lock file exists. '$lock_file'. Exiting\n";
		exit;
	}
	
	open(my $fh_lock, '>>', $lock_file) or die "Could not open '$lock_file' - $!\n";
	close($fh_lock) or die "Could not close '$lock_file' - $!\n";
}

sub release_lock_file
{
	if( length($lock_file) == 0 )
	{
		# No lock file specified ignore it and return
		return;
	}

	print "Releasing lock file ('$lock_file').\n";
	unlink($lock_file);
}
1;